package com.csust.automaticteach.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.knowledgegraph.KnowledgeGraphAddRequest;
import com.csust.automaticteach.model.dto.knowledgegraph.KnowledgeGraphQueryRequest;
import com.csust.automaticteach.model.entity.KnowledgeGraph;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.knowledgegraph.KnowledgeGraphVO;
import com.csust.automaticteach.service.KnowledgeGraphService;
import com.csust.automaticteach.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

/**
 * &#064;ClassName  KnowledgeGraphController
 * &#064;Description  知识图谱控制器
 * &#064;Author钰玟
 * &#064;Date2025/7/19  下午12:22
 * &#064;Version  1.0
 **/
@RestController
@RequestMapping("/knowledgeGraph")
public class KnowledgeGraphController {

    @Resource
    private KnowledgeGraphService knowledgeGraphService;


    @Resource
    private UserService userService;


    /**
     * 添加知识图谱
     *
     * @param knowledgeGraphAddRequest 添加知识图谱
     * @param httpServletRequest       请求
     * @return 添加结果
     */
    @PostMapping("/add")
    public BaseResponse<Boolean> addKnowledgeGraph(@RequestBody KnowledgeGraphAddRequest knowledgeGraphAddRequest
            , HttpServletRequest httpServletRequest) {
        //1.校验参数
        ThrowUtils.throwIf(knowledgeGraphAddRequest == null, ErrorCode.PARAMS_ERROR);
        //2.获取当前用户
        User loginUser = userService.getLoginUser(httpServletRequest);
        //3.生成知识图谱
        Boolean result = knowledgeGraphService.generateKnowledgeGraph(knowledgeGraphAddRequest, loginUser);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);

    }

    /**
     * 删除知识图谱
     *
     * @param deleteRequest      删除参数
     * @param request 请求
     * @return 删除知识图谱是否成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteKnowledgeGraph(@RequestBody DeleteRequest deleteRequest
            , HttpServletRequest request) {
        //1.校验参数
        ThrowUtils.throwIf(deleteRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        KnowledgeGraph getById = knowledgeGraphService.getById(deleteRequest.getId());
        ThrowUtils.throwIf(!loginUser.getId().equals(getById.getUserId()), ErrorCode.OPERATION_ERROR);
        boolean removed = knowledgeGraphService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!removed, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<KnowledgeGraphVO>> listKnowledgeGraphByPage(@RequestBody KnowledgeGraphQueryRequest knowledgeGraphQueryRequest
            , HttpServletRequest httpServletRequest) {
        //1.校验参数
        ThrowUtils.throwIf(knowledgeGraphQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = knowledgeGraphQueryRequest.getCurrent();
        int pageSize = knowledgeGraphQueryRequest.getPageSize();
        User loginUser = userService.getLoginUser(httpServletRequest);
        knowledgeGraphQueryRequest.setUserId(loginUser.getId());
        Page<KnowledgeGraph> knowledgeGraphPage = knowledgeGraphService.page(new Page<>(current, pageSize)
                , knowledgeGraphService.getKnowledgeGraphQueryWrapper(knowledgeGraphQueryRequest));
        Page<KnowledgeGraphVO> knowledgeGraphVOPage = new Page<>(current, pageSize, knowledgeGraphPage.getTotal());
        knowledgeGraphVOPage.setRecords(knowledgeGraphService.getKnowledgeGraphVOList(knowledgeGraphPage.getRecords()));
        return ResultUtils.success(knowledgeGraphVOPage);

    }
}
